package cn.iocoder.yudao.module.resource.controller.admin.advertiser;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.advertiser.vo.ResourceAdvertiserPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.advertiser.vo.ResourceAdvertiserRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.advertiser.vo.ResourceAdvertiserSaveReqVO;
import cn.iocoder.yudao.module.resource.convert.ResourceAdvertiserConvert;
import cn.iocoder.yudao.module.resource.dal.dataobject.advertiser.ResourceAdvertiserDO;
import cn.iocoder.yudao.module.resource.service.advertiser.ResourceAdvertiserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 广告商")
@RestController
@RequestMapping("/resource/advertiser")
@Validated
public class ResourceAdvertiserController {

    @Resource
    private ResourceAdvertiserService advertiserService;

    @PostMapping("/create")
    @Operation(summary = "创建广告商")
    @PreAuthorize("@ss.hasPermission('resource:advertiser:create')")
    public CommonResult<Long> createAdvertiser(@Valid @RequestBody ResourceAdvertiserSaveReqVO createReqVO) {
        return success(advertiserService.createAdvertiser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告商")
    @PreAuthorize("@ss.hasPermission('resource:advertiser:update')")
    public CommonResult<Boolean> updateAdvertiser(@Valid @RequestBody ResourceAdvertiserSaveReqVO updateReqVO) {
        advertiserService.updateAdvertiser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除广告商")
    @Parameter(name = "id", description = "广告商ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:advertiser:delete')")
    public CommonResult<Boolean> deleteAdvertiser(@RequestParam("id") Long id) {
        advertiserService.deleteAdvertiser(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除广告商")
    @Parameter(name = "ids", description = "广告商ID列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('resource:advertiser:delete')")
    public CommonResult<Boolean> deleteAdvertiserList(@RequestParam("ids") List<Long> ids) {
        advertiserService.deleteAdvertiserList(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得广告商")
    @Parameter(name = "id", description = "广告商ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:advertiser:query')")
    public CommonResult<ResourceAdvertiserRespVO> getAdvertiser(@RequestParam("id") Long id) {
        ResourceAdvertiserDO advertiser = advertiserService.getAdvertiser(id);
        return success(BeanUtils.toBean(advertiser, ResourceAdvertiserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得广告商分页")
    @PreAuthorize("@ss.hasPermission('resource:advertiser:query')")
    public CommonResult<PageResult<ResourceAdvertiserRespVO>> getAdvertiserPage(@Valid ResourceAdvertiserPageReqVO pageReqVO) {
        PageResult<ResourceAdvertiserDO> pageResult = advertiserService.getAdvertiserPage(pageReqVO);
        return success(ResourceAdvertiserConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出广告商 Excel")
    @PreAuthorize("@ss.hasPermission('resource:advertiser:export')")
    public void exportAdvertiserExcel(@Valid ResourceAdvertiserPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceAdvertiserDO> list = advertiserService.getAdvertiserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告商.xls", "数据", ResourceAdvertiserRespVO.class,
                BeanUtils.toBean(list, ResourceAdvertiserRespVO.class));
    }

}
